package com.sixks.app.mvc;

import com.sixks.app.api.utils.StringUtil;
import com.sixks.app.dao.CategoryDao;
import com.sixks.app.domain.*;
import com.sixks.app.mvc.cmd.APIRequest;
import com.sixks.app.mvc.cmd.AttributeByCategoryId;
import com.sixks.app.mvc.cmd.CateRequest;
import com.sixks.app.mvc.cmd.CategoryById;
import com.sixks.app.mvc.cmd.exception.ExpressAPIException;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>Title: CategoryController.java </p>
 * <p>Description: </p>
 * <p>Company: Snail Game </p>
 *
 * @author Xinying Ge
 * @date: 03/05/2014
 * @time: 19:22
 */
@Controller
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryDao categoryDao;

    @RequestMapping("/load")
    @ResponseBody
    public List<Category> categoryList(){
        List<Category> categories = this.categoryDao.loadAll();
        return categories;
    }

    //
    @RequestMapping("/attr")
    @ResponseBody
    public List<CategoryAttr> loadAttributes(@RequestParam("cateId") int cateId){
        List<CategoryAttr> attrs = new ArrayList<CategoryAttr>();
        List<AttrName> names = categoryDao.loadByCategoryId(cateId);
        for(AttrName name :names){
            CategoryAttr attr = new CategoryAttr();
            attr.setAttrName(name.getNamesEn());
            attr.setAttrNameId(name.getId());
            List<AttrValue> values = this.categoryDao.loadByAttrNameId(cateId, Integer.valueOf(name.getId()));
            attr.setAttrValueId(values.get(0).getId());
            attr.setAttrValue(values.get(0).getEn());
            attrs.add(attr);
        }
        return attrs;
    }

    @RequestMapping("/dlg")
    public String dlg(){
        return "testdlg";
    }

    @RequestMapping("/cate")
    @ResponseBody
    public JSONResponse getCate(HttpServletRequest request){
        CategoryById cate = new CategoryById();
        ExpressUser user = new ExpressUser();
        user.setAccessToken("216fae64-431e-44c0-a253-6f89466cf944");
        int cateId = 0;
        CateRequest request1 = new CateRequest();
        if(StringUtils.isNotBlank(request.getParameter("cateId"))){
           cateId = NumberUtils.toInt(request.getParameter("cateId"));
            request1.setId(cateId);
        }
        try {
            cate.doCall(request1, user);
        } catch (ExpressAPIException e) {
            e.printStackTrace();
        }
        return new JSONResponse();
    }
    @RequestMapping("/cate/attr")
    @ResponseBody
    public JSONResponse getCateAttr(HttpServletRequest request){
        ExpressUser user = new ExpressUser();
        user.setAccessToken("216fae64-431e-44c0-a253-6f89466cf944");
        AttributeByCategoryId categoryId = new AttributeByCategoryId();
        try {
            categoryId.doCall(null, user);
        } catch (ExpressAPIException e) {
            e.printStackTrace();
        }
        return new JSONResponse();
    }

}
